//
//  SN_QuestionReadMoreNews_Controller.m
//  Shiyun_Newspaper
//
//  Created by zz on 17/6/19.
//  Copyright © 2017年 ShiYun. All rights reserved.
//

#import "SN_QuestionReadMoreNews_Controller.h"

#import "SN_Question_TableViewCell.h"
#import "SN_QuesList.h"
#import "SN_NewspaperInfoViewController.h"
#import "SN_ReadMore_TableHeaderView.h"

@interface SN_QuestionReadMoreNews_Controller () <UITableViewDelegate,UITableViewDataSource,SN_ReadMore_TableHeaderViewDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *dataArr;

@property (nonatomic, strong) UIView *tableHeaderView;

@property (nonatomic, strong) UILabel *emptyLabel;

@end

@implementation SN_QuestionReadMoreNews_Controller {
    
    NSInteger tableHeaderHeight;
}

#pragma mark - LifeCycle

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navBar.navigationItem.title = _subjectTitle;
    
    [self.view addSubview:self.tableView];
    
    tableHeaderHeight = 43; //设置tableviewheaderview的高度
    
    [self requestData]; //请求数据
}

#pragma mark -UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.dataArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    SN_QuesList *model = self.dataArr[section];
    
    return model.isSectionOpen ? model.szblist.count : 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellID = @"cellid";
    
    SN_Question_TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    
    if (!cell) {
        
        cell = [[SN_Question_TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    SN_QuesList *listModel = self.dataArr[indexPath.section];
    
    SN_NewspaperModel *model = listModel.szblist[indexPath.row];
    
    if (indexPath.row == 0) {
        
        model.isFirstIndex = YES;
    }
    
    cell.model = model;
    
    return cell;
}

#pragma mark -UITableViewDelegate
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    SN_ReadMore_TableHeaderView *headerView = [[SN_ReadMore_TableHeaderView alloc] initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, tableHeaderHeight)];
    headerView.delegate = self;
    headerView.section = section;
    headerView.sectionModel = self.dataArr[section];
    
    if (section == 0) { //隐藏第一个分区的线
        headerView.lineView.hidden = YES;
    }
    
    if (section == self.dataArr.count - 1) { //在下部加一条线
        UIView *lineV = [[UIView alloc] init];
        lineV.frame = headerView.lineView.frame;
        lineV.y = headerView.height - 0.5;
        lineV.backgroundColor = headerView.lineView.backgroundColor;
        [headerView addSubview:lineV];
    }
    
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return tableHeaderHeight;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SN_QuesList *listModel = self.dataArr[indexPath.section];
    
    SN_NewspaperModel *model = listModel.szblist[indexPath.row];
    
    if (model.isread == 0) {
    
        model.isread = 1;
        [[listModel.szblist mutableCopy] replaceObjectAtIndex:indexPath.row withObject:model];
        [self.dataArr replaceObjectAtIndex:indexPath.section withObject:listModel];
        [self.tableView reloadData];
    }
    
    
    SN_NewspaperInfoViewController *infoVC = [[SN_NewspaperInfoViewController alloc]init];
    
    infoVC.paperModel = model;
    
    [self.navigationController pushViewController:infoVC animated:YES];
    
    
}


#pragma mark - Public

#pragma mark - Private

#pragma mark -Request
- (void)requestData {
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[@"Method"] = @"api_szb_readmorelist";
    dict[@"editiontype"] = self.editionIdStr;
    dict[@"courseid"] = self.courseIdStr;
    dict[@"gradersid"] = self.gradersIdStr;
    
    [Utils getWithparameters:dict showHUD:YES success:^(NSInteger recode, id jsonObject) {
        
        [self.tableView.mj_header endRefreshing];
        
        NSArray *arr = [jsonObject objectForKey:@"re_data"];
        
        if (arr.count == 0) {
            self.emptyLabel.hidden = NO;
            //空处理
        }else {
            self.emptyLabel.hidden = YES;
        }
        
        self.dataArr = [SN_QuesList mj_objectArrayWithKeyValuesArray:arr];
        SN_QuesList *model = [self.dataArr firstObject];
        model.isSectionOpen = YES;
        [self.dataArr replaceObjectAtIndex:0 withObject:model];
        [self.tableView reloadData];
        
    } failure:^(NSInteger recode, NSError *error) {
        
        [self showHint:error.localizedDescription];
    }];
}

#pragma mark -SN_ReadMore_TableHeaderViewDelegate
- (void)openActionWithSection:(NSInteger)section {
    
    SN_QuesList *model = self.dataArr[section];
    model.isSectionOpen = YES;
    [self.dataArr replaceObjectAtIndex:section withObject:model];
    NSInteger countOfRowsToInsert = model.szblist.count;
    NSMutableArray *indexPathsToInsert = [[NSMutableArray alloc] init];
    for (NSInteger i = 0; i < countOfRowsToInsert; i++) {
        [indexPathsToInsert addObject:[NSIndexPath indexPathForRow:i inSection:section]];
    }
    
    UITableViewRowAnimation animation = UITableViewRowAnimationTop;
    
    [self.tableView beginUpdates];
    [self.tableView insertRowsAtIndexPaths:indexPathsToInsert withRowAnimation:animation];
    
    [self.tableView endUpdates];
}

- (void)closeActionWithSection:(NSInteger)section {
    
    SN_QuesList *model = self.dataArr[section];
    model.isSectionOpen = NO;
    [self.dataArr replaceObjectAtIndex:section withObject:model];
    NSInteger countOfRowsToInsert = model.szblist.count;
    NSMutableArray *indexPathsToInsert = [[NSMutableArray alloc] init];
    for (NSInteger i = 0; i < countOfRowsToInsert; i++) {
        [indexPathsToInsert addObject:[NSIndexPath indexPathForRow:i inSection:section]];
    }
    
    UITableViewRowAnimation animation = UITableViewRowAnimationTop;
    
    [self.tableView beginUpdates];
    [self.tableView deleteRowsAtIndexPaths:indexPathsToInsert withRowAnimation:animation];
    [self.tableView endUpdates];
}

#pragma mark - Getter
- (UITableView *)tableView {
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, SafeAreaTopHeight, VIEW_WIDTH, VIEW_HEIGHT - SafeAreaTopHeight - SafeAreaBottomHeight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = [UIView new];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        
        _tableView.tableHeaderView = self.tableHeaderView;
        
//        [_tableView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)]];
        
        _tableView.sectionHeaderHeight = tableHeaderHeight;  //headerview高度
        _tableView.rowHeight = 34;
        
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            
                [self requestData];
        }];
        
        _emptyLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, SafeAreaTopHeight, VIEW_WIDTH, 100)];
        _emptyLabel.font = [UIFont systemFontOfSize:15];
        _emptyLabel.text = @"暂无数据。";
        _emptyLabel.textColor = [UIColor colorWithHexString:@"#979797"];
        _emptyLabel.textAlignment = NSTextAlignmentCenter;
        _emptyLabel.hidden = YES;
        _emptyLabel.center = self.view.center;
        [_tableView addSubview:_emptyLabel];
    }
    
    return _tableView;
}

- (NSMutableArray *)dataArr {
    
    if (!_dataArr) {
        
        _dataArr = [NSMutableArray array];
    }
    
    return _dataArr;
}

- (UIView *)tableHeaderView {
    
    if (!_tableHeaderView) {
        
        _tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, 55)];
        _tableHeaderView.backgroundColor = [UIColor whiteColor];
        
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 14, 150, 17)];
        titleLabel.font = [UIFont systemFontOfSize:17];
        titleLabel.textColor = [UIColor colorWithHexString:@"#3b3b3b"];
        titleLabel.text = _editionStr ? _editionStr : @"一年级数学";
        [_tableHeaderView addSubview:titleLabel];
        
        UIView *grayView = [[UIView alloc] initWithFrame:CGRectMake(0, 45, VIEW_WIDTH, 10)];
        grayView.backgroundColor = [UIColor colorWithHexString:@"#f5f4f7"];
        [_tableHeaderView addSubview:grayView];
    }
    
    return _tableHeaderView;
}

#pragma mark - Setter

@end

